package com.bj58.data.emailreports.sends;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.bj58.data.emailreports.controllers.CPCOpenHourController;
import com.bj58.data.emailreports.dao.SrcDbPool.DBType;
import com.bj58.data.emailreports.model.EmailReportUser;
import com.bj58.data.emailreports.utils.DateUtil;
import com.bj58.data.emailreports.dao.CommonDao;

public class CPCHourSend extends BaseSend{
	
	private static CommonDao comDao = new CommonDao();

	public static void main(String[] args) {
		CPCHourSend  cpcHourSend = new CPCHourSend();
		CPCOpenHourController  cpcHourController = new CPCOpenHourController();
		String searchDate = DateUtil.getStringDate(new Date(),"yyyy-MM-dd");
		int hour = 0;
		String beginTime="";
		String endTime="";
		
		try {
			if(null != args && args.length > 0){
				if(args.length != 2 || !args[0].contains("-")){
					System.out.println("useage: mainClass [yyyy-MM-DD 0-23] 。");
					System.exit(0);
				}
				searchDate = args[0];
				hour = Integer.parseInt(args[1]);
			}else{
				searchDate = DateUtil.getStringDate(new Date(),"yyyy-MM-dd");
				hour = Integer.parseInt(DateUtil.getStringDate(new Date(), "HH"));
				if(hour == 0){
					hour = 23;
					searchDate = DateUtil.strToStrBefore(searchDate, "yyyy-MM-dd", 1);
				}else{
					hour = hour - 1; //一小时前
				}
			}
			
			if(hour <0 || hour > 23){
				System.out.println("useage: mainClass [yyyy-MM-DD 0-23] 。");
				System.exit(0);
			}
				
			long maxWait = 30*60*1000; //最多等30分钟
			while(maxWait > 0){
				System.out.println("begin check source table :"+searchDate+","+hour);
				
				String month = searchDate.substring(0,searchDate.lastIndexOf("-")).replace("-", "");
				String sql="select count(1) from data_bi.t_jingzhun_user_click_aduserantispam_hour_"+month +" where stat_date ='"+searchDate+"' and stat_hour = "+hour ;
				
				int count = comDao.countAll(sql,DBType.db_131_data_bi.getValue());
				if(count > 0){
					System.out.println("source table has "+count+" recodes!");
					break;
				}
				System.out.println("source table is not ready then sleep for 1 minites!");
				maxWait -= 60*1000;
				Thread.sleep(60*1000);
			}
			if(maxWait<=0)
			{
				System.exit(0);
			}
			
			beginTime = DateUtil.getStringDate(DateUtil.strToDate(hour+":00", "HH:mm"), "HH:mm");
			endTime = DateUtil.getStringDate(DateUtil.strToDate((hour+1)+":00", "HH:mm"), "HH:mm");
			
			//CPC每小时数据监控日报
			List<EmailReportUser> userListCpc = cpcHourSend.getUsersBySendType(46);
			String content = cpcHourController.cpcOpenHourReport(searchDate,hour);
			if(StringUtils.isBlank(content)){
				cpcHourSend.errorWarn("【报错】CPC每小时数据监控日报" +searchDate +" "+beginTime+"--"+endTime);
			}else{
				cpcHourSend.ReportSend(userListCpc,"CPC每小时数据监控日报" +searchDate +" "+beginTime+"--"+endTime, content, null);
			}
			System.out.println("CPC每小时数据监控日报" +searchDate +" "+beginTime+"--"+endTime);
		} catch (Exception e) {
			e.printStackTrace();
			cpcHourSend.errorWarn("CPC每小时数据监控日报" +searchDate +" "+beginTime+"--"+endTime);
		}
	}

}
